package com.example.springboot2.oa.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot2.base.web.BaseController;
import com.example.springboot2.oa.entity.TCustomer;
import com.example.springboot2.oa.entity.TDepartment;
import com.example.springboot2.oa.entity.TStudent;
import com.example.springboot2.oa.entity.TUser;
import com.example.springboot2.oa.service.ITCustomerService;
import com.example.springboot2.oa.service.ITUserService;
import com.example.springboot2.tools.MakeResponse;
import com.example.springboot2.tools.ResponseMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

/**
 * <p>
 *  用户控制层
 * </p>
 *
 * @author llfy
 * @since 2018-11-23
 */
@CrossOrigin
@RestController
@RequestMapping("/oa/t-customer")
public class TCustomerController extends BaseController {

    /**
     *注入userService
     */
    @Autowired
    private ITCustomerService customerSerive;

    /**
     * 用户列表
     * @param request
     * @return
     */
    @PostMapping("listAll")
    public ResponseMessage listAll(HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TCustomer> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("create_time");
        List<TCustomer> data = customerSerive.list(wrapper);
        return MakeResponse.success("200", "成功", data);
    }

    /**
     * 添加用户
     */
    @PostMapping("add")
    public ResponseMessage add(@RequestBody TCustomer customer, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        customer.setCreateTime(new Date());

        if (!customerSerive.save(customer)) {
            return MakeResponse.success("500", "新增失败");
        }

        return MakeResponse.success("200", "新增成功");
    }

    /**
     * 批量删除用户
     * @param customerList
     * @param request
     * @return
     */
    @PostMapping("delete")
    public ResponseMessage delete(@RequestBody List<TCustomer> customerList, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        List<Integer> customerIdList = new ArrayList();
        customerList.forEach(new Consumer<TCustomer>() {
            @Override
            public void accept(TCustomer customer) {
                customerIdList.add(customer.getId());
            }
        });

        if (!customerSerive.removeByIds(customerIdList)) {
            return MakeResponse.success("500", "批量删除失败");
        }

        return MakeResponse.success("200", "批量删除成功");
    }

    /**
     * 更新用户
     * @param user
     * @param request
     * @return
     */
    @PostMapping("modify")
    public ResponseMessage modify(@RequestBody TCustomer customer, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        if (!customerSerive.updateById(customer)) {
            return MakeResponse.success("500", "修改失败");
        }

        return MakeResponse.success("200", "修改成功");
    }
}
